package com.surfilter.mq.rocketmq;

import com.surfilter.mq.rocketmq.cache.RockMqMesgManage;
import com.surfilter.mq.rocketmq.cache.RocketMqMessageStandard;
import com.surfilter.mq.rocketmq.exception.OprateException;
import com.surfilter.mq.threadman.ThreadManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public abstract class RocketMqMessageStandardOprate implements Runnable,Cloneable{
	
	private static final Logger logger = LoggerFactory.getLogger(RocketMqMessageStandardOprate.class);
	
	private RocketMqMessageStandard mesg;
	
	private RockMqMesgManage manage;
	
	private ThreadManager threadmanager;
	
	public RocketMqMessageStandardOprate(RocketMqMessageStandard mesg, RockMqMesgManage manage){
		this.manage = manage;
		this.mesg = mesg;
	}

	public abstract void opreateRocketMqMessageStandard(RocketMqMessageStandard mesg)throws OprateException;

	@Override
	public void run() {
		// TODO Auto-generated method stub
		execute(true);
	}
	
	public void execute(boolean clear_thread){
		try {
			logger.info("线程：{}，开始处理任务:{}", Thread.currentThread(), mesg.getMesgKey());
			opreateRocketMqMessageStandard(mesg);
			logger.info("线程：{}，处理任务:{} 完成", Thread.currentThread(), mesg.getMesgKey());
		} catch (OprateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("处理任务:{} 异常", mesg.getMesgKey(), e);
		}finally{
			manage.removeMesg(mesg.getMesgKey());
			if(clear_thread)threadmanager.remove(Thread.currentThread().getName());
		}
	}
	
	public RocketMqMessageStandard getMesg() {
		return mesg;
	}

	public void setMesg(RocketMqMessageStandard mesg) {
		this.mesg = mesg;
	}

	public RockMqMesgManage getManage() {
		return manage;
	}

	public void setManage(RockMqMesgManage manage) {
		this.manage = manage;
	}

	public ThreadManager getThreadmanager() {
		return threadmanager;
	}

	public void setThreadmanager(ThreadManager threadmanager) {
		this.threadmanager = threadmanager;
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		// TODO Auto-generated method stub
		return super.clone();
	}
	
}
